package com.suli.multimedia.service.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.suli.multimedia.entity.Ridership;
import com.suli.multimedia.entity.SalesQuantity;
import com.suli.multimedia.entity.ScenicTicket;
import com.suli.multimedia.entity.UserTicket;
import com.suli.multimedia.enums.Status;
import com.suli.multimedia.mapper.RidershipMapper;
import com.suli.multimedia.mapper.SalesQuantityMapper;
import com.suli.multimedia.mapper.ScenicTicketMapper;
import com.suli.multimedia.mapper.UserTicketMapper;
import com.suli.multimedia.service.UserTicketService;
import com.suli.multimedia.util.GetDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author SuLi
 * @since 2022-04-04
 */
@Service
@Transactional
public class UserTicketServiceImpl extends ServiceImpl<UserTicketMapper, UserTicket> implements UserTicketService {

    @Autowired
    UserTicketMapper userTicketMapper;
    @Autowired
    RidershipMapper ridershipMapper;
    @Autowired
    ScenicTicketMapper scenicTicketMapper;
    @Autowired
    SalesQuantityMapper salesQuantityMapper;

    @Override
    public Map<String, Object> getUserTicketList(String status, String userId, Long limit, Long page) {
        Page<UserTicket> userTicketPage = new Page<>(page, limit);
        QueryWrapper<UserTicket> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userId).eq("ticket_status", Status.valueOf(status));
        Page<UserTicket> userTicketPage1 = userTicketMapper.selectPage(userTicketPage, queryWrapper);
        if(status.equals("UN_USE")){
            List<UserTicket> records = userTicketPage1.getRecords();
            records.forEach(record ->{
                long nowTime = new Date().getTime();
                long enterTime = record.getTicketExpireTime().getTime()+1800000L;
                if(nowTime>enterTime){
                    record.setTicketStatus(Status.EXPIRE);
                    userTicketMapper.updateById(record);
                }
            });
            Page<UserTicket> userTicketPage2 = userTicketMapper.selectPage(userTicketPage, queryWrapper);
            Map<String, Object> map=new HashMap<>();
            map.put("total",userTicketPage2.getTotal());
            map.put("ticketList",userTicketPage2.getRecords());
            return map;
        }
        if(status.equals("USING")){
            List<UserTicket> records = userTicketPage1.getRecords();
            records.forEach(record ->{
                long  nowTime = new Date().getTime();
                long lastTime = GetDate.getTomorrowMorning(record.getTicketExpireTime()).getTime();
                if(nowTime>lastTime){
                    record.setTicketStatus(Status.USED);
                    userTicketMapper.updateById(record);
                }
            });
            Page<UserTicket> userTicketPage2 = userTicketMapper.selectPage(userTicketPage, queryWrapper);
            Map<String, Object> map=new HashMap<>();
            map.put("total",userTicketPage2.getTotal());
            map.put("ticketList",userTicketPage2.getRecords());
            return map;
        }else {
            Map<String, Object> map=new HashMap<>();
            map.put("total",userTicketPage1.getTotal());
            map.put("ticketList",userTicketPage1.getRecords());
            return map;
        }
    }

    @Override
    public void updateStatus(String ticketId, String status) {
        UserTicket userTicket=new UserTicket();
        userTicket.setId(ticketId);
        userTicket.setTicketStatus(Status.valueOf(status));
        userTicketMapper.updateById(userTicket);
        try {
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            String format = sdf.format(new Date());
            Date enterTime = sdf.parse(format);
            Ridership ridership = ridershipMapper.selectById(enterTime);
            if(ObjectUtils.isEmpty(ridership)){
                ridership=new Ridership();
                ridership.setEnterDate(enterTime);
                ridership.setQuality(1);
                ridershipMapper.insert(ridership);
            }else {
                ridership.setQuality(ridership.getQuality()+1);
                ridershipMapper.updateById(ridership);
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean returnTicketOne(UserTicket userTicket) {
        if(userTicket.getTicketStatus()!=Status.UN_USE){
            return false;
        }
        int i = userTicketMapper.deleteById(userTicket.getId());
        int i1 = salesQuantityMapper.updateSalesAndSalesVolumeByBuyDate(userTicket.getTicketPrice(), userTicket.getTicketBuyTime());
        ScenicTicket scenicTicket = scenicTicketMapper.selectById(userTicket.getTicketId());
        scenicTicket.setScenicTicketSale(scenicTicket.getScenicTicketSale()-1);
        scenicTicket.setScenicTicketNumber(scenicTicket.getScenicTicketNumber()+1);
        int update = scenicTicketMapper.updateById(scenicTicket);
        return update >= 1 && i >= 1 && i1 >= 1;

    }
}
